<?php
namespace app\admin\controller;

use app\common\service\PostClass as PostClassService;
use think\Controller;
use think\Request;

/**
 * 文章分类管理
 * Class PostClass
 * @package app\admin\controller
 */
class PostClass extends Controller{

    public function index(){

        $postClassService = new PostClassService();
        $postClass = $postClassService -> paginate();

        return view('',['postClass'=>$postClass]);
    }

    public function add(){

        return view();
    }

    public function create(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only('name');

        $postClassService = new PostClassService();
        $result = $postClassService ->insert($data);

        if($result['state']) {
            $this->success("操作成功");
        }else{
            $this->error($result['message']);
        }
    }

    public function edit(Request $request){
        $input = $request->only(['id']);

        $id = intval($input['id']);

        $postClassService = new PostClassService();
        $postClass = $postClassService -> find($id);

        return view('',['postClass'=>$postClass]);
    }

    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['id','name']);

        $postClassService = new PostClassService();
        $result = $postClassService ->update($data);

        if(1 == $result['state']) {
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }

    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法访问途径");
        }

        $id = $request->only(['id']);
        if(count($id) !== 1){
            $this->error("禁止批量操作");
        }

        $postClassService = new PostClassService();
        $count = $postClassService -> delete($id);

        if($count){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }
}